 <template>
  <div>
    <el-upload
      :action="action"
      :data="dataObj"
      list-type="picture-card"
      :file-list="fileList"
      :before-upload="beforeUpload"
      :on-remove="handleRemove"
      :on-success="handleUploadSuccess"
      :on-preview="handlePreview"
      :limit="maxCount"
      :on-exceed="handleExceed"
    >
      <i class="el-icon-plus"></i>
    </el-upload>
    <el-dialog :visible.sync="dialogVisible">
      <img width="100%" :src="dialogImageUrl" alt="">
    </el-dialog>
  </div>
</template>
<script>
    import draggable from 'vuedraggable'
  import {policy} from '@/api/oss'
  import utils from '../../utils/utils'
  export default {
    name: 'multiUpload',
    props: {
      //图片属性数组
      value: Array,
      //最大上传图片数量
      maxCount:{
        type:Number,
        default:5
      }
    },
    data() {
      return {
        dataObj: {
          policy: '',
          signature: '',
          key: '',
          ossaccessKeyId: '',
          dir: '',
          host: ''
        },
        action:'',
        dialogVisible: false,
        dialogImageUrl:null,
        fileList:[],
        first:true,
        uuid:'',
      };
    },
    watch: {
      value: {
        handler(newVal, oldVal) {
          if(this.value != null && this.value !== '' && this.first){
            this.first = false;//用变量控制第一次才改变值
            let fileList=[];
            for(let i=0;i<newVal.length;i++){
              fileList.push({name:newVal[i].substr(newVal[i].lastIndexOf("/") + 1),url:newVal[i]});
            }
            this.fileList = fileList
          }
        },
//        immediate: true
      },
    },
    methods: {
      emitInput(fileList) {
        console.log('111')
        let value=[];
        console.log(fileList)
        for(let i=0;i<fileList.length;i++){
          console.log(i)
          if (i+1 == fileList.length){
            fileList[i].name = this.uuid+fileList[i].name
          }
          value.push(this.dataObj.host + '/' + this.dataObj.dir + '/'+ fileList[i].name);
        }
        this.$emit('input', value)
        console.log(value)
      },
      handleRemove(file, fileList) {
        let value=[];
        for(let i=0;i<fileList.length;i++){
          value.push(fileList[i].url);
        }
        this.$emit('input', value)
      },
      handlePreview(file) {
        console.log('333')
        this.dialogVisible = true;
        this.dialogImageUrl=file.url;
      },
      beforeUpload(file) {
        let fileSize = (file.size/1024/1024).toFixed(2)
        if(fileSize > 3){
          this.$message.warning(`图片过大，请上传3M以内的图片`)
          return false
        }
        console.log('444')
        let _self = this;
        _self.uuid = utils.getUUID()
        return new Promise((resolve, reject) => {
          policy().then(response => {
              console.info("上传图片")
              console.info(response)
            _self.dataObj.policy = response.data.policy;
            _self.dataObj.signature = response.data.signature;
            _self.dataObj.ossaccessKeyId = response.data.accessKeyId;
            _self.dataObj.dir = response.data.dir;
            _self.dataObj.key = response.data.dir+'/'+_self.uuid+"${filename}";
            _self.dataObj.host = response.data.host;
            _self.action = response.data.action;
            resolve(true)
          }).catch(err => {
            console.log(err)
            reject(false)
          })
        })
      },
      handleUploadSuccess(res, file,fileList) {
        console.log('5555')
        this.fileList = fileList;
        this.emitInput(this.fileList);
        console.log(fileList)
      },
      handleExceed(files, fileList) {
        this.$message({
          message: '最多只能上传'+this.maxCount+'张图片',
          type: 'warning',
          duration:1000
        });
      },
    }
  }
</script>
<style>

</style>


